/**
 * 角色管理（添加|修改）roleOption.js
 * 版本号： v1.0.0
 */
$(function(){

    //设置输入框的最大输入长度
    $("#name").textbox("textbox").attr("maxlength", 18);
    $("#href").textbox("textbox").attr("maxlength", 202);
    $("#target").textbox("textbox").attr("maxlength", 22);
    $("#sort").textbox("textbox").attr("maxlength", 12);
    $("#permission").textbox("textbox").attr("maxlength", 38);
    $("#remark").textbox("textbox").attr("maxlength", 202);

    //操作按钮点击
    $('#optionMenu').bind('click', function(){
        doOptionMenuMethod();
    });

    //为父级菜单设置树形下拉选项
    var parentId = $("#parentId").val();
    $.ajax({
        url: CONTEXT_PATH + "/menu/getTreeMenu.do",
        type: "get",
        success: function (data) {
            var themecombo = "";
            if(data.length > 2){
                themecombo = "[{\"text\": \"根节点菜单\", \"id\": \"0\"},";
                themecombo += data.substring(1).replace(/meId/g,"id").replace(/name/g,"text");
            }else{
                themecombo = "[{\"text\": \"根节点菜单\", \"id\": \"0\"}]";
            }
            themecombo = JSON.parse(themecombo);
            $("#menuTree").data("menuList",themecombo);
            $("#menuTree").combotree("loadData",themecombo);
            $("#menuTree").combotree({
                onSelect:function(node){
                    $('#parentId').val(node.id);
                }
            });
            $("#menuTree").combotree("setValue","0");
            if(parentId){
                $("#menuTree").combotree("setValue",parentId);
            }
        }
    });

    //选择菜单图标按钮弹出对话框
    $("#chooseIcon").click(function(){
        $("#chooseIcon_dialog").dialog({
            title: '选择图标',
            width: 383,
            modal: true,
            closed: true,
            height: 360,
            buttons:[{
                text:"确定",
                iconCls:"icon-role-state-ok",
                handler:function(){
                    $('#chooseIcon_dialog').dialog('close');
                }
            },{
                text:"取消",
                iconCls:"icon-role-state-no",
                handler:function(){
                    $('#chooseIcon_dialog').dialog('close');
                }
            }]
        });
        $("#chooseIcon_dialog").dialog("move", {left:420, top:200});
        $("#chooseIcon_dialog").dialog('open');
    });

    //菜单小图标点击事件
    $(".iconDiv").click(function(){
        $(".iconDiv").removeClass("selected");
        $(this).addClass("selected");
        var iconCls = $(this).attr("icon-name");
        $("#icon-to-show").linkbutton({ iconCls:iconCls });
        $("#iconCls").val(iconCls);
    });

});

/**
 * 执行保存操作方法
 */
function doOptionMenuMethod(){
    var params = $("#operate_form").serialize();
    $("#operate_form").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/menu/save.do",
        onSubmit:function() {
            var flag = $(this).form("validate");
            if(flag){
                preventSubmit(null, "optionMenu");
            }
            return flag;
        },
        success:function(data) {
            var obj = JSON.parse(data);
            var url = CONTEXT_PATH + "/menu/index.do";
            saveJump(obj,url);
        }
    });
}